package com.lagou.edu.homework.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @program: stage-3-module-2
 * @description:
 * @author: zhangxiaoyang
 * @date: 2020-08-26 01:16
 **/
@Component
@Import(GetNodeData.class)
public class DataSourceConfig {


    @Autowired
    private GetNodeData getNodeData;

    private static DruidDataSource instance = new DruidDataSource();

    @Bean
    @Lazy
    public DruidDataSource dataSource() throws IOException, InterruptedException {
        getNodeData.initZK(getNodeData);
        Map<String, String> dataSourceMap = GetNodeData.dataSourceMap;
        instance.setDriverClassName(dataSourceMap.get("driver-class-name"));
        instance.setUrl(dataSourceMap.get("url"));
        instance.setUsername(dataSourceMap.get("username"));
        instance.setPassword(dataSourceMap.get("password"));
        return instance;
    }

    public DruidDataSource getDataSource() {
        return instance;
    }

    public void setDataSource(DruidDataSource dataSource) {
        instance = dataSource;
    }


}
